//
//  SEGFindQuizListByQuizTypeModel.m
//  ExcellentHome
//
//  Created by 刘山 on 16/8/29.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGFindQuizListByQuizTypeModel.h"
#if __has_include(<Commom/SEGServerManager.h>)
#import <Commom/SEGServerManager.h>
#else
#import "SEGServerManager.h"
#endif
#if __has_include(<Commom/SEGUtils.h>)
#import <Commom/SEGUtils.h>
#else
#import "SEGUtils.h"
#endif
#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

@implementation SEGFindQuizListByQuizTypeModel
+ (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    return [[self alloc] initWithDictionary:dictionary];
}

- (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dictionary];
        //设置layout
        [self saveLayout];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString*)key
{
}

-(instancetype)init{
    if (self == [super init]) {
        _levelUrl = @"";
    }
    return self;
}

- (void)setQuizId:(NSString*)quizId
{
    if ([quizId isKindOfClass:[NSNumber class]]) {
        _quizId = [[NSString stringWithFormat:@"%@", quizId] copy];
        return;
    }
    _quizId = quizId;
}

- (void)setLinkTilte:(NSString*)linkTilte
{
    _linkTilte = [linkTilte copy];
    _linkTitle = [linkTilte copy];
}

-(void)setLinkTitle:(NSString *)linkTitle{
    _linkTitle = [linkTitle copy];
    if (!_linkTilte) {
        _linkTilte = [linkTitle copy];
    }
}

- (void)setQuizPic:(NSString*)quizPic
{
    _quizPic = [quizPic copy];

    NSArray* arr = @[];

    if ([_quizPic rangeOfString:@","].length > 0) {
        arr = [[_quizPic componentsSeparatedByString:@","] copy];
    }
    else if (_quizPic.length > 0) {
        arr = @[ _quizPic ];
    }

    NSMutableArray* arrM = [NSMutableArray array];
    NSMutableArray* arrM2 = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL* _Nonnull stop) {

        if ([obj length] > 0) {
            [arrM addObject:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, obj]];
            [arrM2 addObject:obj];
        }

    }];

    self.quizPicArr = [arrM copy];
    self.noRootPicArr = [arrM2 copy];
}

- (BOOL)showNoDataPic
{
    if (!_showNoDataPic) {
        _showNoDataPic = NO;
    }
    return _showNoDataPic;
}

-(void)setLevelUrl:(NSString *)levelUrl{
    _levelUrl = [SEGUtils ValueToString:levelUrl];
}

- (void)setPgcType:(NSString *)pgcType{
    _pgcType = [[SEGUtils ValueToString:pgcType]copy];
}

- (CGFloat)imageHeightWithName:(NSString *)imageName width:(CGFloat)width {
    /*
     1 新图：
     1.1 最大比例：3:4
     1.2 最小比例：2:1
     2 老图
     2.1 默认：16:9
     */
    CGFloat height;
    
    CGFloat newMaxScale = 1.333;// 3:4
    CGFloat newMinScale = 0.5;// 2:1
    
    NSArray *data = [imageName componentsSeparatedByString:@"_"];
    
    if (data.count == 3) {//新图
        CGFloat picWith = [data[1] floatValue];
        CGFloat picHeight = [[data[2] componentsSeparatedByString:@"."].firstObject floatValue];
        CGFloat scale = picHeight/picWith;
        scale = scale > newMaxScale ? newMaxScale : scale;
        scale = scale < newMinScale ? newMinScale : scale;
        height = width * scale;
    }else{//旧图
        height = width * 0.5625;//16:9
    }
    return height;
}

- (void)saveLayout
{
    SEGcustomContentViewModel *data = [[SEGcustomContentViewModel alloc] init];
    data.widgetWidth = LYScreenWidth() - kNeighbourTextLeftPadding - 15;//宽度
    data.like = ValueToString(_praiseTotal);
    data.comment = @"";//_model.commentCount;
    if ([_objType isEqualToString:@"5"]) { // 话题
        if ([_pgcType isEqualToString:@"2"]) {//图文话题
            data.contentText = _quizContent;//quizContent1还没赋值，quizContent还没修改
            SEGTopicPicViewModel *dataSource = [[SEGTopicPicViewModel alloc] init];
            dataSource.topicTitle = _linkTitle;
            if (_quizPicArr.count > 0) {
                dataSource.picUrlStrArray = _quizPicArr;
            }
            data.topicPicDataSource = dataSource;
            data.isDetailView = YES;
            _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_TOPIC_PHOTOText];
        }else {//文字话题
            SEGTopicTextViewModel *dataSource = [[SEGTopicTextViewModel alloc] init];
            dataSource.content = _quizContent;//quizContent1还没赋值，quizContent还没修改
            dataSource.topicTitle = _linkTitle;
            data.topicTextDataSource = dataSource;
            data.isDetailView = YES;
            _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_TOPIC_TEXTONLY];
        }
    }else {
        SEGNeighbourViewModel *NeighbourDataSource = [[SEGNeighbourViewModel alloc] init];
        NeighbourDataSource.type = SEGEnumNeighbourViewType_Neighbour;
        
        if ([_isBroadCast integerValue] > 0) {//求助
            NSMutableString *content = [NSMutableString stringWithString:_quizContent];
            if (_contacts && _contacts.length > 0) {
                [content appendFormat:@"\n联系人：%@",_contacts];
            }
            if (_tel && _tel.length > 0) {
                [content appendFormat:@"\n联系方式：%@",_tel];
            }
            data.contentText = content;
            data.helpLabelText = _labelName;
            //旧数据如有多张图片数据，默认取第一张
            if (_quizPicArr && _quizPicArr.count > 1) {
                NeighbourDataSource.imageContent = [_quizPicArr subarrayWithRange:NSMakeRange(0, 1)];
            }else {
                NeighbourDataSource.imageContent = _quizPicArr;
            }
        }else {
            data.contentText = _quizContent;
            NeighbourDataSource.imageContent = _quizPicArr;
        }
        data.NeighbourDataSource = NeighbourDataSource;
        data.isDetailView = YES;
        _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_NEIGHBOUR_PHOTOText];
    }
}

@end
